/**
 * This library is a thing to realize D2L and DI and AOP on Flex2(ActionScript3).
 * Copyright (C) 2007 Takamasa Seki
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Contributor : Takamasa Seki(takanafu@gmail.com)
 *               http://www.takanafu.com/
 */

package com.takanafu.haruframework.d2l{
	import com.takanafu.core.haru_internal;
	
	import mx.managers.SystemManager;
	import mx.rpc.http.HTTPService;
	
	import mx.core.mx_internal;
	import mx.core.Singleton;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import flash.display.DisplayObject;
	import mx.core.UIComponentDescriptor;
	import mx.core.Container;
	import mx.core.Singleton;
	import flash.utils.getDefinitionByName;
	
	import com.takanafu.haruframework.d2l.invoker.InvocationProxyImpl;
	
	use namespace haru_internal;
	use namespace mx_internal;

	public class HaRuSystemManager extends SystemManager{
		public var appContextURL:String = "ApplicationContext.xml";
		private var _isLoaded:Boolean = false;
		private var _isInit:Boolean = false;
		
		public function HaRuSystemManager(){
			trace("[HaRu] Start HaRuFramework initialization...");
			var a:InvocationProxyImpl;
			Singleton.registerClass("com.takanafu.haruframework.d2l.invoker::IInvocationProxy",Class(flash.utils.getDefinitionByName("com.takanafu.haruframework.d2l.invoker.InvocationProxyImpl")));
	    	var srv:HTTPService = new HTTPService();
	        
	        trace("[HaRu] Loading ApplicationContextFile : "+this.appContextURL);
        	srv.url = this.appContextURL;
        	srv.addEventListener("result",xmlResultHandler);
            srv.addEventListener("fault",xmlErrorHandler);
            srv.useProxy = false;
            srv.resultFormat = "e4x";
            srv.send();
            super();
		}
		
		private function xmlResultHandler(result:ResultEvent):void{
	    	trace("[HaRu] Load Successful ApplicationContextFile");
	    	HaRu.appContextData = result.result as XML;
	    	
			_isLoaded = true;
	    	if(_isInit == false){
	    		super.initialize();
	    	}
	    }
	    private function xmlErrorHandler(result:FaultEvent):void{
	    	trace("[HaRu] Load Error ApplicationContextFile. cause "+result.fault.faultDetail);
	    	_isLoaded = true;
	    	super.initialize();
	    }
	    
	    override mx_internal function initialize():void{
	    	if(_isLoaded == true){
	    		_isInit = true;
	    		super.initialize();
	    	}
	    }
	    
	}
}